package com.yyds.blackmagic.controller;


import com.yyds.blackmagic.entity.Book;
import com.yyds.blackmagic.service.BookService;
import com.yyds.blackmagic.vo.ActionResult;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName BookController
 * @Auther Dell
 * @Date 2021/12/13 17:26
 **/
@RestController
@Slf4j
@RequestMapping("/api")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("/BookList")
    public ActionResult selectAll(){
        List<Book> books = bookService.selectAll();

        return new ActionResult(200,null,books);
    }

    @GetMapping("/BookOne/{cid}")
    public ActionResult selectOne(@PathVariable("cid") int cid){
        if(cid!=0) {
            List<Book> list = bookService.selectOne(cid);
            return new ActionResult(200, "onesuccess", list);
        }else {
            return selectAll();
        }
    }

    @PutMapping("/BookAdd")
    public ActionResult adds(@RequestBody Book book){
        Book add = bookService.add( book);
        if(add!=null){
            return new ActionResult(200,null,book);
        }else{
            return new ActionResult(-200,"addfalse",book);
        }
    }



    @PutMapping("/BookUpdate")
    public ActionResult update(@RequestBody Book book){
        Book update = bookService.update(book);
            return new ActionResult(200,null ,book);
    }

    @GetMapping("/selectbyname")
    public ActionResult select(@RequestParam("name") String name){
        log.info("333");
        List<Book> select = bookService.select(name);
        return new ActionResult(200,null,select);
    }

    @GetMapping("/search")
    public ActionResult search(@RequestParam("keywords") String keywords) {
        log.info("121");
        List<Book> select = bookService.select(keywords);
        return new ActionResult(200,null,select);
    }

    @GetMapping("/selectBydate")
    public ActionResult selectBydate() {
        List<Book> list = bookService.selectBydate();
        return new ActionResult(200, null, list);

    }
    @DeleteMapping("/BookDel/{id}")
    public ActionResult del(@PathVariable("id") int id){
        int a = bookService.delete(id);
        if(a>-1){
            return new ActionResult(200,null, "SUCCESSFUL");
        }
        return new ActionResult(-200,null,"false");
    }

//    @GetMapping("/searchBook")
//    public ActionResult searchById(@RequestParam("id") int id) {
//        Book search = bookService.search(id);
//        return new ActionResult(200, null, search);
//    }
    @GetMapping("/searchBook/{id}")
    public ActionResult searchById(@PathVariable("id") int id) {
        Book search = bookService.search(id);
        return new ActionResult(200, null, search);
}

}
